package com.quectel.core.module.partybuilding.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partybuilding.dao.PartyBuildingServicesDao;
import com.quectel.core.module.partybuilding.dto.PartyBuildingServicesDto;
import com.quectel.core.module.partybuilding.entity.PartyBuildingServicesEntity;
import com.quectel.core.module.partybuilding.service.PartyBuildingServicesService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:38
 */
@DubboService
public class PartyBuildingServicesServiceImpl implements PartyBuildingServicesService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyBuildingServicesServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARTY_BUILDING_SERVICES_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARTY_BUILDING_SERVICES_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PartyBuildingServicesDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARTY_BUILDING_SERVICES_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PartyBuildingServicesDao partyBuildingServicesDao;


    /**
     * @param tenantId
     * @param category
     * @return
     */
    @Override
    public PartyBuildingServicesDto selectOneByCategory(Long tenantId, Byte category) {
        PartyBuildingServicesEntity servicesEntity = partyBuildingServicesDao.selectOne(new LambdaQueryWrapper<PartyBuildingServicesEntity>()
                .eq(PartyBuildingServicesEntity::getTenantId, tenantId)
                .eq(PartyBuildingServicesEntity::getCategory, category)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        return paddingField(CopyUtils.copyObj(servicesEntity, PartyBuildingServicesDto.class));
    }

    @Override
    public PartyBuildingServicesDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARTY_BUILDING_SERVICES_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PartyBuildingServicesDto selectById(Long id){
            PartyBuildingServicesEntity entity = partyBuildingServicesDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PartyBuildingServicesDto.class));
    }

    @Override
    public List<PartyBuildingServicesDto> queryList(Map<String,Object> params){
        List<PartyBuildingServicesEntity> list = partyBuildingServicesDao.queryList(params);
        List<PartyBuildingServicesDto> result =  CopyUtils.copyList(list,PartyBuildingServicesDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PartyBuildingServicesDto paddingField(PartyBuildingServicesDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return partyBuildingServicesDao.queryTotal(params);
    }

    @Override
    public Long save(PartyBuildingServicesDto dto){
        PartyBuildingServicesEntity entity = CopyUtils.copyObj(dto,PartyBuildingServicesEntity.class);
        entity.setId(Snowflake.nextId());
        partyBuildingServicesDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PartyBuildingServicesDto dto){
        PartyBuildingServicesEntity entity = CopyUtils.copyObj(dto,PartyBuildingServicesEntity.class);

        partyBuildingServicesDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PartyBuildingServicesDto dto) {
       PartyBuildingServicesEntity entity = CopyUtils.copyObj(dto,PartyBuildingServicesEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, partyBuildingServicesDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        partyBuildingServicesDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
